<?php

namespace App\Http\Controllers;

use App\Services\PermissionService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

/**
 * 权限控制器
 * 提供权限管理相关的API接口
 */
class PermissionController extends Controller
{
    public function __construct(
        private PermissionService $permissionService
    ) {}

    /**
     * 获取当前用户权限列表
     */
    public function getUserPermissions(): JsonResponse
    {
        $user = Auth::user();

        if (!$user) {
            return response()->json([
                'success' => false,
                'message' => '用户未登录'
            ], 401);
        }

        $permissions = $this->permissionService->getUserPermissions($user);

        return response()->json([
            'success' => true,
            'data' => $permissions,
            'message' => '获取用户权限成功'
        ]);
    }

    /**
     * 检查用户是否有指定权限
     */
    public function checkPermission(Request $request): JsonResponse
    {
        $request->validate([
            'permission' => 'required|string'
        ]);

        $user = Auth::user();

        if (!$user) {
            return response()->json([
                'success' => false,
                'message' => '用户未登录'
            ], 401);
        }

        $permission = $request->input('permission');
        $hasPermission = $this->permissionService->hasPermission($user, $permission);

        return response()->json([
            'success' => true,
            'data' => [
                'permission' => $permission,
                'has_permission' => $hasPermission
            ],
            'message' => $hasPermission ? '用户拥有该权限' : '用户没有该权限'
        ]);
    }

    /**
     * 检查用户是否有任意权限
     */
    public function checkAnyPermission(Request $request): JsonResponse
    {
        $request->validate([
            'permissions' => 'required|array',
            'permissions.*' => 'string'
        ]);

        $user = Auth::user();

        if (!$user) {
            return response()->json([
                'success' => false,
                'message' => '用户未登录'
            ], 401);
        }

        $permissions = $request->input('permissions');
        $hasAnyPermission = $this->permissionService->hasAnyPermission($user, $permissions);

        return response()->json([
            'success' => true,
            'data' => [
                'permissions' => $permissions,
                'has_any_permission' => $hasAnyPermission
            ],
            'message' => $hasAnyPermission ? '用户拥有其中一个权限' : '用户没有这些权限'
        ]);
    }

    /**
     * 检查用户是否有所有权限
     */
    public function checkAllPermissions(Request $request): JsonResponse
    {
        $request->validate([
            'permissions' => 'required|array',
            'permissions.*' => 'string'
        ]);

        $user = Auth::user();

        if (!$user) {
            return response()->json([
                'success' => false,
                'message' => '用户未登录'
            ], 401);
        }

        $permissions = $request->input('permissions');
        $hasAllPermissions = $this->permissionService->hasAllPermissions($user, $permissions);

        return response()->json([
            'success' => true,
            'data' => [
                'permissions' => $permissions,
                'has_all_permissions' => $hasAllPermissions
            ],
            'message' => $hasAllPermissions ? '用户拥有所有权限' : '用户缺少某些权限'
        ]);
    }

    /**
     * 获取所有权限列表
     */
    public function getAllPermissions(): JsonResponse
    {
        $permissions = $this->permissionService->getAllPermissions();

        return response()->json([
            'success' => true,
            'data' => $permissions,
            'message' => '获取所有权限成功'
        ]);
    }

    /**
     * 获取权限配置详情
     */
    public function getPermissionConfig(string $permission): JsonResponse
    {
        $config = $this->permissionService->getPermissionConfig($permission);

        if (!$config) {
            return response()->json([
                'success' => false,
                'message' => '权限不存在'
            ], 404);
        }

        return response()->json([
            'success' => true,
            'data' => array_merge($config, ['key' => $permission]),
            'message' => '获取权限配置成功'
        ]);
    }

    /**
     * 获取所有模块配置
     */
    public function getAllModules(): JsonResponse
    {
        $modules = $this->permissionService->getAllModules();

        return response()->json([
            'success' => true,
            'data' => $modules,
            'message' => '获取所有模块成功'
        ]);
    }

    /**
     * 获取模块配置
     */
    public function getModuleConfig(string $module): JsonResponse
    {
        $config = $this->permissionService->getModuleConfig($module);

        if (!$config) {
            return response()->json([
                'success' => false,
                'message' => '模块不存在'
            ], 404);
        }

        return response()->json([
            'success' => true,
            'data' => array_merge($config, ['key' => $module]),
            'message' => '获取模块配置成功'
        ]);
    }

    /**
     * 获取所有角色配置
     */
    public function getAllRoles(): JsonResponse
    {
        $roles = $this->permissionService->getAllRoles();

        return response()->json([
            'success' => true,
            'data' => $roles,
            'message' => '获取所有角色成功'
        ]);
    }

    /**
     * 获取用户菜单
     */
    public function getUserMenus(): JsonResponse
    {
        $user = Auth::user();

        if (!$user) {
            return response()->json([
                'success' => false,
                'message' => '用户未登录'
            ], 401);
        }

        $menus = $this->permissionService->getUserMenus($user);

        return response()->json([
            'success' => true,
            'data' => $menus,
            'message' => '获取用户菜单成功'
        ]);
    }

    /**
     * 刷新权限缓存
     */
    public function refreshCache(): JsonResponse
    {
        $this->permissionService->refreshCache();

        return response()->json([
            'success' => true,
            'message' => '权限缓存刷新成功'
        ]);
    }

    /**
     * 获取路由权限
     */
    public function getRoutePermission(Request $request): JsonResponse
    {
        $request->validate([
            'method' => 'required|string',
            'uri' => 'required|string'
        ]);

        $method = $request->input('method');
        $uri = $request->input('uri');

        $permission = $this->permissionService->getRoutePermission($method, $uri);

        return response()->json([
            'success' => true,
            'data' => [
                'method' => $method,
                'uri' => $uri,
                'permission' => $permission
            ],
            'message' => $permission ? '找到路由权限' : '路由未配置权限'
        ]);
    }
}
